package br.edu.fatecou.reserva.dao;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.edu.fatecou.reserva.dominio.Categoria;
import br.edu.fatecou.reserva.util.HibernateUtil;

public class CategoriaDAO {
	public void salvar(Categoria categoria) throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Transaction transacao = sessao.beginTransaction();
		sessao.saveOrUpdate(categoria);
		transacao.commit();
		sessao.close();
	}

	public void editar(Categoria categoria) throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Transaction transacao = sessao.beginTransaction();
		sessao.update(categoria);
		transacao.commit();
		sessao.close();
	}

	public void excluir(Categoria categoria) throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();
		Transaction transacao = sessao.beginTransaction();
		sessao.delete(categoria);
		transacao.commit();
		sessao.close();
	}

	public Categoria buscar(Long codigo) throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();

		Criteria consulta = sessao.createCriteria(Categoria.class);
		consulta.add(Restrictions.idEq(codigo));

		Categoria resultado = (Categoria) consulta.uniqueResult();
		sessao.close();

		return resultado;
	}

	@SuppressWarnings("unchecked")
	public List<Categoria> buscar(String nome) throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();

		Criteria consulta = sessao.createCriteria(Categoria.class);
		consulta.add(Restrictions.like("nome", "%" + nome + "%"));
		consulta.addOrder(Order.asc("nome"));

		List<Categoria> lista = consulta.list();
		sessao.close();

		return lista;
	}
	
	/**
	 * Metodo para carregar as Categorias
	 * @return ArrayList<SelectItem>
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ArrayList<SelectItem> getComboCategoria() throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();

		Criteria consulta = sessao.createCriteria(Categoria.class);
		
		ArrayList<SelectItem> newList = new ArrayList<SelectItem>();
		List<Categoria> lista = consulta.list();
						
		for (Categoria l : lista) {
			SelectItem item = new SelectItem(l, l.getNome());
			newList.add(item);
		}
		
		sessao.close();

		return newList;
	}

	@SuppressWarnings("unchecked")
	public List<Categoria> buscar(String nome, Integer codigo)
			throws Exception {
		Session sessao = HibernateUtil.getSessionFactory().openSession();

		Criteria consulta = sessao.createCriteria(Categoria.class);

		Conjunction e = Restrictions.conjunction();
		e.add(Restrictions.eq("nome", nome));
		e.add(Restrictions.ne("codigo", codigo));

		consulta.add(e);

		List<Categoria> lista = consulta.list();
		sessao.close();

		return lista;
	}
}
